package com.qiguliuxing.dts.core.notify;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.qiguliuxing.dts.db.domain.DtsUserFormid;
import com.qiguliuxing.dts.db.service.DtsUserFormIdService;
import cn.binarywang.wx.miniapp.api.WxMaService;

/**
 * 微信小程序模版消息通知
 */
public class WxTemplateSender {
	private final Log logger = LogFactory.getLog(WxTemplateSender.class);

	@Autowired
	private WxMaService wxMaService;

	@Autowired
	private DtsUserFormIdService formIdService;

	/**
	 * 发送微信消息(模板消息),不带跳转
	 *
	 * @param touser
	 *            用户 OpenID
	 * @param templatId
	 *            模板消息ID
	 * @param parms
	 *            详细内容
	 */
	public void sendWechatMsg(NotifyType notifyType,String touser, String templatId, String[] parms) {
		sendMsg(notifyType,touser, templatId, parms, "", "");
	}

	/**
	 * 发送微信消息(模板消息),带跳转
	 *
	 * @param touser
	 *            用户 OpenID
	 * @param templatId
	 *            模板消息ID
	 * @param parms
	 *            详细内容
	 * @param page
	 *            跳转页面
	 */
	public void sendWechatMsg(NotifyType notifyType,String touser, String templatId, String[] parms, String page) {
		sendMsg(notifyType,touser, templatId, parms, page, "");
	}

	private void sendMsg(NotifyType notifyType,String touser, String templatId, String[] parms, String page,
			String emphasisKeyword) {
		DtsUserFormid userFormid = formIdService.queryByOpenId(touser);
		if (userFormid == null)
			return;
		WxMaSubscribeMessage msg = new WxMaSubscribeMessage();
		msg.setTemplateId(templatId);
		msg.setToUser(touser);
		msg.setPage(page);
		msg.setLang("zh CN");
		//developer为开发版;trial为体验版;formal为正式版;默认为正式版
		msg.setMiniprogramState("developer");
		msg.setData(createMsgData(notifyType,parms));

		try {
			wxMaService.getMsgService().sendSubscribeMsg(msg);
			if (formIdService.updateUserFormId(userFormid) == 0) {
				logger.warn("更新数据已失效");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<WxMaSubscribeMessage.MsgData> createMsgData(NotifyType notifyType, String[] parms) {
		logger.info("消息类别:{"+notifyType.getType()+"}");
		new WxMaSubscribeMessage.MsgData();
		List<WxMaSubscribeMessage.MsgData> dataList = new ArrayList<WxMaSubscribeMessage.MsgData>();
		switch (notifyType.getType()){
			case "paySucceed":
				dataList = Arrays.asList(
						new WxMaSubscribeMessage.MsgData("character_string1", parms[0]),//订单号
						new WxMaSubscribeMessage.MsgData("amount4", parms[1]),//订单金额
						new WxMaSubscribeMessage.MsgData("time8", parms[2]),//下单时间
						new WxMaSubscribeMessage.MsgData("thing9", parms[3]),//收货地址
						new WxMaSubscribeMessage.MsgData("phone_number10", parms[4])//联系电话
						);
				break;
			case "ship":
				dataList = Arrays.asList(
						new WxMaSubscribeMessage.MsgData("character_string1", parms[0]),//订单编号
						new WxMaSubscribeMessage.MsgData("amount2", parms[1]),//订单金额
						new WxMaSubscribeMessage.MsgData("character_string5", parms[2]),//快递单号
						new WxMaSubscribeMessage.MsgData("thing20", parms[3]),//快递公司
						new WxMaSubscribeMessage.MsgData("time9", parms[4])//发货时间
				);
				break;
			case "applyRefund":
				dataList = Arrays.asList(
				);
				break;
			case "refund":
				dataList = Arrays.asList(
						new WxMaSubscribeMessage.MsgData("character_string4", parms[0]),//订单编号
						new WxMaSubscribeMessage.MsgData("amount1", parms[1]),//退款金额
						new WxMaSubscribeMessage.MsgData("time2", parms[2])//退款时间
				);
				break;
			default:
				break;
		}
		return dataList;
	}
}
